package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @Classname AutoFillAspect
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/6/21 17:40
 * @Created by Ren
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointPut() {
        log.info("进入切入点 autoFillPointPut...");
    }

    /**
     * 前置通知 在方法执行前 执行的函数
     */
    @Before("autoFillPointPut()")
    public void autoFill(JoinPoint joinPoint) {

        log.info("进入 autoFill 前置通知方法");

        //获取到 方法的类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();  //获取方法签名对象

        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);  //获取到方法上面的接口

        OperationType operationType = annotation.value();   //获取到方法类型

        //获取到传入的参数

        Object[] args = joinPoint.getArgs();
        Object entity = args[0];

        if (args == null || args.length == 0) {
            return; //没有参数
        }

        //准备好赋值的数据

        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        //根据不同的方法类型通过反射给属性赋值
        if (operationType.equals(OperationType.INSERT)) {

            Method set_create_time = null;
            try {

                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                setCreateTime.invoke(entity, now);        //创建时间赋值
                setCreateUser.invoke(entity, currentId);  //创建人赋值
                setUpdateTime.invoke(entity, now);        //修改时间赋值
                setUpdateUser.invoke(entity, currentId);  //修改人赋值

            } catch (Exception e) {
                log.info("自动字段赋值失败 = {}", e.getMessage());
            }

        }


        if (operationType.equals(OperationType.UPDATE)) {

            try {

                Method set_update_time = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method set_update_user = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                set_update_time.invoke(entity, now);  //修改时间赋值
                set_update_user.invoke(entity, currentId);  //修改人赋值

            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
    }
}
